//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BlackVarianceSurface.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/Equityfx/BlackVarianceTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Math;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ strikes, Cephei::QL::Math::IMatrix^ blackVolMatrix, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Termstructures::Volatility::Equityfx::BlackVarianceSurface::ExtrapolationEnum>^ lowerExtrapolation, Microsoft::FSharp::Core::FSharpOption<QL::Termstructures::Volatility::Equityfx::BlackVarianceSurface::ExtrapolationEnum>^ upperExtrapolation) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
    CCalendar^ _Ccal;
    CoVector<DateTime>^ _Cdates;
    CoVector<Double>^ _Cstrikes;
    CMatrix^ _CblackVolMatrix;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phBlackVarianceSurface = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        CoVector<DateTime>^ _Cdates = safe_cast<CoVector<DateTime>^> (dates);
        _Cdates->Lock();
        INativeVector<DateTime>^ _NCIdates = _Cdates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCdates = safe_cast<CDateTimeVector^>(_NCIdates);
        std::vector<QuantLib::Date>& _dates = static_cast<std::vector<QuantLib::Date>&> (_NCdates->GetReference ());
        CoVector<Double>^ _Cstrikes = safe_cast<CoVector<Double>^> (strikes);
        _Cstrikes->Lock();
        INativeVector<Double>^ _NCIstrikes = _Cstrikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCstrikes = safe_cast<CDoubleVector^>(_NCIstrikes);
        std::vector<QuantLib::Real>& _strikes = static_cast<std::vector<QuantLib::Real>&> (_NCstrikes->GetReference ());
        _CblackVolMatrix = safe_cast<CMatrix^> (blackVolMatrix);
        _CblackVolMatrix->Lock();
        QuantLib::Matrix& _blackVolMatrix = static_cast<QuantLib::Matrix&> (_CblackVolMatrix->GetReference ()); 
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::BlackVarianceSurface::Extrapolation _lowerExtrapolation = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Termstructures::Volatility::Equityfx::BlackVarianceSurface::ExtrapolationEnum>::IsSome::get (lowerExtrapolation) ? (QuantLib::BlackVarianceSurface::Extrapolation)lowerExtrapolation->Value : QuantLib::BlackVarianceSurface::Extrapolation::InterpolatorDefaultExtrapolation); //10
        QuantLib::BlackVarianceSurface::Extrapolation _upperExtrapolation = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Termstructures::Volatility::Equityfx::BlackVarianceSurface::ExtrapolationEnum>::IsSome::get (upperExtrapolation) ? (QuantLib::BlackVarianceSurface::Extrapolation)upperExtrapolation->Value : QuantLib::BlackVarianceSurface::Extrapolation::InterpolatorDefaultExtrapolation); //10
        _ppBlackVarianceSurface = new boost::shared_ptr<QuantLib::BlackVarianceSurface> (new QuantLib::BlackVarianceSurface ( _referenceDate,  _cal,  _dates,  _strikes,  _blackVolMatrix,  _dayCounter,  _lowerExtrapolation,  _upperExtrapolation ));
        SetBlackVarianceTermStructure (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cdates != nullptr) _Cdates->Unlock();
        if (_Cstrikes != nullptr) _Cstrikes->Unlock();
        if (_CblackVolMatrix != nullptr) _CblackVolMatrix->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (boost::shared_ptr<QuantLib::BlackVarianceSurface>& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	_ppBlackVarianceSurface = &childNative;
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::BlackVarianceSurface& childNative, Object^ owner) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	_ppBlackVarianceSurface = new boost::shared_ptr<QuantLib::BlackVarianceSurface> (&childNative);
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
    _BlackVarianceSurfaceOwner = owner;
    _BlackVarianceTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (CBlackVarianceSurface^ copy) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackVarianceSurface = new boost::shared_ptr<QuantLib::BlackVarianceSurface> (copy->GetShared());
        _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (System::Type^ t) : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	if (!t->IsSubclassOf(CBlackVarianceSurface::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::Handle<QuantLib::BlackVarianceSurface>& childNative, Object^ owner)  : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
	_phBlackVarianceSurface = &childNative;
	_ppBlackVarianceSurface = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceSurface>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
    _BlackVarianceSurfaceOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::Handle<QuantLib::BlackVarianceSurface> childNative)  : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
	_phBlackVarianceSurface = &childNative;
	_ppBlackVarianceSurface = &static_cast<boost::shared_ptr<QuantLib::BlackVarianceSurface>>(childNative.currentLink());
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::CBlackVarianceSurface (QuantLib::BlackVarianceSurface childNative)  : CBlackVarianceTermStructure(CBlackVarianceSurface::typeid)
{
#ifdef HANDLE
	_phBlackVarianceSurface = NULL;
#endif
	_ppBlackVarianceSurface = new boost::shared_ptr<QuantLib::BlackVarianceSurface> (new QuantLib::BlackVarianceSurface (childNative));
    _ppBlackVarianceTermStructure = new boost::shared_ptr<QuantLib::BlackVarianceTermStructure> (boost::dynamic_pointer_cast<QuantLib::BlackVarianceTermStructure> (*_ppBlackVarianceSurface));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::~CBlackVarianceSurface ()
{
    if (_ppBlackVarianceSurface != NULL)
    {
	    delete _ppBlackVarianceSurface;
        _ppBlackVarianceSurface = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::!CBlackVarianceSurface ()
{
    if (_ppBlackVarianceSurface != NULL)
    {
	    delete _ppBlackVarianceSurface;
    }
}
QuantLib::BlackVarianceSurface& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetReference ()
{
    if (_ppBlackVarianceSurface == NULL) throw gcnew NativeNullException ();
	return **_ppBlackVarianceSurface;
}
boost::shared_ptr<QuantLib::BlackVarianceSurface>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetShared ()
{
    if (_ppBlackVarianceSurface == NULL) throw gcnew NativeNullException ();
	return *_ppBlackVarianceSurface;
}
QuantLib::BlackVarianceSurface* Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetPointer ()
{
    if (_ppBlackVarianceSurface == NULL) throw gcnew NativeNullException ();
	return &**_ppBlackVarianceSurface;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackVarianceSurface>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::GetHandle ()
{
	if (_phBlackVarianceSurface == NULL)
	{
		_phBlackVarianceSurface = new Handle<QuantLib::BlackVarianceSurface> (*_ppBlackVarianceSurface);
	}
	return *_phBlackVarianceSurface;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::HasNative () 
{
	return (_ppBlackVarianceSurface != NULL);
}

Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppBlackVarianceSurface)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppBlackVarianceSurface)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceSurface)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVarianceSurface)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Equityfx::IBlackVarianceSurface^ Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVarianceSurface_Factory::Create (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, Cephei::IVector<DateTime>^ dates, Cephei::IVector<Double>^ strikes, Cephei::QL::Math::IMatrix^ blackVolMatrix, Cephei::QL::Times::IDayCounter^ dayCounter, Microsoft::FSharp::Core::FSharpOption<QL::Termstructures::Volatility::Equityfx::BlackVarianceSurface::ExtrapolationEnum>^ lowerExtrapolation, Microsoft::FSharp::Core::FSharpOption<QL::Termstructures::Volatility::Equityfx::BlackVarianceSurface::ExtrapolationEnum>^ upperExtrapolation)
{
    return gcnew CBlackVarianceSurface ( referenceDate,  cal,  dates,  strikes,  blackVolMatrix,  dayCounter,  lowerExtrapolation,  upperExtrapolation);
}
